/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.android.music.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.Shader.TileMode;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.android.music.MusicApplication;
import com.android.music.R;
import com.android.music.model.LrcObject;
import com.android.music.util.Util;

/**
 * 歌词秀面板
 * 
 * @author LeiKang
 */
public final class ViewLrc extends View {

    /**
     * 绘制歌词面板
     */
    private TextPaint paint;

    /**
     * 歌词映射，每句歌词对应list中的一项
     */
    private List<LrcObject> lrc_map;

    /**
     * 当前播放位置
     */
    private int mCurrentPosition;

    /**
     * 根据当前播放行大致确定歌词向上滚动位置
     */
    private float playingY = 150;

    /**
     * 歌词向上缓慢滚动值
     */
    private float movingY = 0;

    /**
     * 歌词X轴位置
     */
    private float playingX = 0;

    /**
     * 当前播放行
     */
    private int playingLine;

    /**
     * 当前行的宽度
     */
    private float currentLineWidth;

    /**
     * 水平方向歌词颜色显示设置
     */
    private Shader shaderX = null;

    private int sizeOfText;

    private int sizeOfHeight;

    public ViewLrc(Context context, AttributeSet attrs) {
        super(context, attrs);
        /* 歌词面板绘制 paint */
        paint = new TextPaint();
        sizeOfText = (int) this.getResources().getDimension(R.dimen.text_size);
        sizeOfHeight = (int) this.getResources().getDimension(R.dimen.text_height);
        paint.setTextSize(sizeOfText);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTypeface(Typeface.SANS_SERIF);
        paint.setAntiAlias(true);
        lrc_map = new ArrayList<LrcObject>();
    }

    /**
     * 设置当前播放位置
     * 
     * @param position
     */
    public void setlrcPosition(int position) {
        lrc_map = MusicApplication.getInstance().getLrcMap();
        if (lrc_map.size() > 1) {
            mCurrentPosition = position + 500;
            findTheLrc();
            this.postInvalidate();
        } else
            playingY = 0;
    }

    public void init() {
        playingY = 0;
        movingY = 0;
        playingX = 0;
    }

    @Override
    public void onDraw(Canvas canvas) {

        for (int i = 0; i < lrc_map.size(); i++) {
            LrcObject val = (LrcObject) lrc_map.get(i);
            paint.setColor(Util.LRC_BACKGROUND_COLOR);
            if (i == playingLine) {
                paint.setShader(shaderX);
                paint.setColor(Util.LRC_PLAYING_COLOR);
            }
            canvas.drawText(val.lrc, getWidth() / 2, MusicApplication.getInstance().getLrcCenter()
                    - playingY + i * sizeOfHeight - movingY, paint);
            paint.setShader(null);
        }
    }

    private void findTheLrc() {
        /** 选出当前播放行 */
        for (int i = 0; i < lrc_map.size(); i++) {
            LrcObject val = (LrcObject) lrc_map.get(i);
            if (mCurrentPosition < (val.begintime + val.timeline)) {
                playingLine = i;
                /** 首行所在位置 */
                playingY = sizeOfHeight * playingLine;
                break;
            }
        }
        Rect rect = new Rect();
        paint.setTextSize(sizeOfText);
        if (lrc_map.size() == 0)
            return;
        paint.getTextBounds(lrc_map.get(playingLine).lrc, 0, 1, rect);
        currentLineWidth = paint.measureText(lrc_map.get(playingLine).lrc);

        /** ===确定播放 X 坐标 如果不需要 渐变 ，直接将playingX设为最大值，movingY设为0=== */
        playingX = 4 * (mCurrentPosition - lrc_map.get(playingLine).begintime) * currentLineWidth
                / lrc_map.get(playingLine).timeline;
        if (MusicApplication.getInstance().getSharedPreference().getBoolean(Util.IS_LRC_KTV))
            playingX = 4 * getWidth();

        shaderX = new LinearGradient(0, 0, playingX, 0, new int[] {
                Util.LRC_PLAYING_COLOR, Util.LRC_BACKGROUND_COLOR
        }, new float[] {
                (float) 0.5, (float) 0.51
        }, TileMode.CLAMP);

        /** ===================== 确定播放 Y 坐标 ================================== */

        movingY = (mCurrentPosition - lrc_map.get(playingLine).begintime) * sizeOfHeight
                / lrc_map.get(playingLine).timeline;
        if (playingLine == lrc_map.size() - 1)
            movingY = 0;
        // movingY = 0;
    }
}
